from models import db, BaseModel


class HostGroup(BaseModel):
    __tablename__ = 'host_group'
    name = db.Column(db.String(64), unique=True, comment='组名称')
    hosts = db.relationship('Host', backref=db.backref('host_group'))

    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return f'<Group {self.name}>'


class Host(BaseModel):
    __tablename__ = 'host'
    name = db.Column(db.String(64), unique=True, comment='主机名称')
    ip = db.Column(db.String(24), unique=True, comment='IP地址')
    port = db.Column(db.Integer, comment='端口号')
    username = db.Column(db.String(64), comment='用户名')
    password = db.Column(db.String(128), comment='用户密码')
    auth_type = db.Column(db.Enum('password', 'ssh_key'), server_default='password', comment='验证类型')
    ssh_key_file = db.Column(db.Text, default="", comment='SSH KEY File')
    host_group_id = db.Column(db.Integer, db.ForeignKey('host_group.id'))

    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return f'<Host {self.name}>'
